<template>
  <div class="login-container">
    <div class="login-form">
        <h3 class="login-title">小U商城后台管理系统</h3>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item  prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off">
              <template slot="prepend">
                  <i class="el-icon-user"></i>
              </template>
          </el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off" :show-password="true">
              <template slot="prepend">
                  <i class="el-icon-lock"></i>
              </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn-login" type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message:'请输入用户名', trigger: "blur" }],
        password: [{ required: true, message:'请输入登录密码', trigger: "blur" }],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            //   alert("submit!");
            // 此处请求数据接口
            this.$http.post('/api/userlogin',this.ruleForm).then(res=>{
                if(res.code==200){
                    // 登录成功
                    this.$message({
                        type:'success',
                        message:'登录成功'
                    });
                    // 1-将服务端返回的信息记录到sessionStorage中
                    sessionStorage.setItem('userinfo',JSON.stringify(res.list));
                    // 2-页面重定向
                    return this.$router.push('/');
                }
                // 给出失败提示
                this.$message({
                    type:'error',
                    message:'登录失败'
                });

            })

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style>
.login-container {
  /* vh   vw */
  width: 100vw;
  height: 100vh;
  background: url("../assets/img/loginbg.jpeg");
  background-size: cover;
  /* 启动flex弹性布局 */
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-form {
  width: 398px;
  height: 294px;
  background: #FFF;
  border-radius:4px;
  padding:30px ;
  box-shadow: 0 0 4px 2px #666;  
}
.login-title{
    text-align: center;
    color:#FB6D49;
    margin-bottom: 30px;
}
.btn-login{
    width: 100%;
}
</style>